// pages/movie/movie-more/movie-more.js
let app = getApp();
let utils = require("../../util/utils.js")

Page({

    /**
     * 页面的初始数据
     */
    data: {
        movies: [],
        totalCount: 0,
        totalMovies: [],
        isEmpty: true,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        const categoryName = options.categoryname;
        this.setData({
            categoryName: categoryName
        })

        let publicUrl = app.globalUrl.doubanUrl;

        let allUrl = "";
        // console.log(options.categoryname)
        switch (options.categoryname) {
            case "正在热映":
                allUrl = publicUrl + "/j/search_subjects?type=movie&tag=%E7%83%AD%E9%97%A8";
                break;
            case "即将上映":
                allUrl = publicUrl + "/j/search_subjects?type=movie&tag=%E7%83%AD%E9%97%A8";
                break;
            case "排行榜":
                allUrl = publicUrl + "/j/search_subjects?type=movie&tag=%E7%83%AD%E9%97%A8";
                break;
        }
        this.setData({
            allUrl: allUrl
        })

        // 进行网络请求
        utils.http(allUrl, this.callback)
        // 加载进行中，给用户良好的体验
        wx.showNavigationBarLoading()
    },

    // 下拉刷新
    onPullDownRefresh: function () {
        // console.log('下拉触发事件')
        let refreshUrl = this.data.allUrl
        this.data.totalMovies = []
        this.data.isEmpty = true
        utils.http(refreshUrl, this.callback)
    },

    // 上拉加载
    onReachBottom: function (event) {
        // console.log('上划触发事件')
        let nextUrl = app.globalUrl.doubanUrl + "/j/search_subjects?type=movie&tag=%E7%83%AD%E9%97%A8&sort=recommend&page_limit=20&page_start=" + this.data.totalCount
        wx.showNavigationBarLoading()
        utils.http(nextUrl, this.callback)
    },

    callback: function (res) {
        // console.log(res)
        let movies = []
        for (let idx in res.subjects) {
            let subject = res.subjects[idx];

            let title = subject.title;
            if (title.length >= 6) {
                //  名字过长处理
                title = title.substring(0, 6) + "...";
            }

            let temp = {
                title: title,
                coverImgUrl: subject.cover,
                star: utils.convertToStarsArray(subject.rate * 10 / 2),
                average: subject.rate,
                movieid: subject.id
            }
            movies.push(temp)
        }
        let totalMovies = []
        /**
         * concat: 合并数组
         * 是不是第一次进入： 第一次进入是不需要累加的
         * 非第一次进入的时候累加
         */
        if (!this.data.isEmpty) {
            // 非第一次进入
            // 将所有请求到的数组组合在一起
            totalMovies = this.data.movies.concat(movies)
        } else {
            // 第一次进入
            totalMovies = movies
            this.data.isEmpty = false
        }
        this.setData({
            movies: totalMovies
        })
        // console.log(movies)
        this.data.totalCount += 20
        // 加载完成，结束加载提醒
        wx.hideNavigationBarLoading()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
        wx.setNavigationBarTitle({
            title: this.data.categoryName
        })
    },


    goMovieDetail: function (event) {
        let movieId = event.currentTarget.dataset.movieid
        wx.navigateTo({
            url: '../movie-detail/movie-detail?movieid=' + movieId,
        })
    },

})